//
//  SelectionViewController.m
//  礼物
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "SelectionViewController.h"

#import <AFNetworking.h>
#import <MJRefresh.h>
#import "CycleModel.h"
#import "ImageViewModel.h"
#import "ImageViewController.h"
@interface SelectionViewController ()

@end

@implementation SelectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectionView=[[SelectionView alloc]initWithFrame:MAINSCREEN];
    self.selectionView.delegate=self;
  [self.view addSubview:self.selectionView];
[AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/banners?channel=iOS"
                             andBlock:^(id data){
                                 self.selectionView.lunboArray = [NSMutableArray array];
                                 NSMutableDictionary *dictionary = data;
                                 NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"banners"];
        for (NSMutableDictionary *dic in array)
            {
                CycleModel *cycleModel = [[CycleModel alloc] init];
                NSString *type=[dic objectForKey:@"type"];
                if ([type isEqualToString:@"collection"] )
                {
                    
                    cycleModel.target_id = [[dic objectForKey:@"target_id"]integerValue];
                    cycleModel.image_url = [dic objectForKey:@"image_url"];
                    cycleModel.target_url=[dic objectForKey:@"target_url"];
                    [self.selectionView.lunboArray addObject:cycleModel];
            }
    }
    [self.selectionView setLunboPicture];
    }andError:^(id failure) {}];
[AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/promotions?gender=1&generation=1" andBlock:^(id data) {
    self.selectionView.imageViewArray=[NSMutableArray array];
    NSMutableDictionary *Dic=data;
    NSMutableArray *array=[[Dic objectForKey:@"data"] objectForKey:@"promotions"];
    NSMutableArray *array1=[NSMutableArray arrayWithObjects:[array objectAtIndex:0],[array objectAtIndex:1], nil];
          ImageViewModel *model1=[[ImageViewModel alloc]init];
           ImageViewModel *model2=[[ImageViewModel alloc]init];
        [model1 setValuesForKeysWithDictionary:[array1 objectAtIndex:0]];
      [model2 setValuesForKeysWithDictionary:[array1 objectAtIndex:1]];
    [self.selectionView.imageViewArray addObject:model1];
    [self.selectionView.imageViewArray addObject:model2];
    [self.selectionView setImageV:self.selectionView.imageViewArray];
    
} andError:^(id failure) {}];

    self.selectionView.firstPageCellArray=[NSMutableArray array];
    NSArray *fileArray1 =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath1 = [fileArray1 lastObject];
    NSString *archivePath1 =[documentPath1 stringByAppendingPathComponent:@"selection"];
    NSMutableDictionary *data = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath1];
    NSMutableDictionary *Dic=data;
    NSMutableArray *array=[[Dic objectForKey:@"data"]objectForKey:@"items"];
    for (NSMutableDictionary *dic in array) {
        Model *model=[[Model alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.selectionView.firstPageCellArray addObject:model];
    }
    [self.selectionView.myTableView reloadData];
    self.currentPage=20;
    [self GetAFNetWorkingData];
    
    self.HUD=[[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.mode=MBProgressHUDModeIndeterminate;
    self.HUD.delegate=self;
    self.HUD.labelText=@"正在加载";
    self.HUD=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}

-(void)GetAFNetWorkingData
{
    [self addHeaderRefresh];
    [self addFooterRefresh];
}
//设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.selectionView.myTableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/channels/111/items?ad=1&gender=1&generation=2&limit=20&offset=0" andBlock:^(id data) {
            self.HUD.hidden=YES;
            self.selectionView.firstPageCellArray=[NSMutableArray array];
            NSFileManager *fileManager=[NSFileManager defaultManager];
            NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath=[fileArray lastObject];
            NSString *archivePath =[documentPath stringByAppendingPathComponent:@"selection"];
            BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
            NSLog(@"==== %@",archivePath);
            if (relust1) {
                NSLog(@"沙盒文件删除成功");
            }else
            {
                NSLog(@"沙盒文件删除失败");
            }
            BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
            if (relust4) {
                NSLog(@"归档成功");
            }else
            {
                NSLog(@"归档失败");
            }
            
            NSMutableDictionary *Dic=data;
            NSMutableArray *array=[[Dic objectForKey:@"data"]objectForKey:@"items"];
            for (NSMutableDictionary *dic in array) {
                Model *model=[[Model alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.selectionView.firstPageCellArray addObject:model];
            }
            [self.selectionView.myTableView reloadData];
            [self.selectionView.myTableView.header endRefreshing];
        } andError:^(id failure) {
            self.HUD.hidden=YES;
            [self.selectionView.myTableView.header endRefreshing];
            UIAlertView *aView=[[UIAlertView alloc]initWithTitle:@"刷新失败" message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aView show];
        }];
    }];
    [self.selectionView.myTableView.header beginRefreshing];
}

-(void)addFooterRefresh
{
    //当上拉时候会触发该方法
    self.selectionView.myTableView.footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.currentPage+=20;
        [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/111/items?ad=1&gender=1&generation=2&limit=%ld&offset=0",self.currentPage]
                              andBlock:^(id data){
                                  self.selectionView.firstPageCellArray=[NSMutableArray array];
                                  NSFileManager *fileManager=[NSFileManager defaultManager];
                                  NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                  NSString *documentPath=[fileArray lastObject];
                                  NSString *archivePath =[documentPath stringByAppendingPathComponent:@"selection"];
                                  BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
                                  NSLog(@"==== %@",archivePath);
                                  if (relust1) {
                                      NSLog(@"沙盒文件删除成功");
                                  }else
                                  {
                                      NSLog(@"沙盒文件删除失败");
                                  }
                                  BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
                                  if (relust4) {
                                      NSLog(@"归档成功");
                                  }else
                                  {
                                      NSLog(@"归档失败");
                                  }
                                  NSMutableDictionary *Dic=data;
                                  NSMutableArray *array=[[Dic objectForKey:@"data"]objectForKey:@"items"];
                                  for (NSMutableDictionary *dic in array) {
                                      Model *model=[[Model alloc]init];
                                      [model setValuesForKeysWithDictionary:dic];
                                      [self.selectionView.firstPageCellArray addObject:model];
                                  }
                                  [self.selectionView.myTableView reloadData];
                                  [self.selectionView.myTableView.footer endRefreshing];
                              } andError:^(id failure) {
                                  [self.selectionView.myTableView.footer endRefreshing];
                              }];
        
    }];
    
    
}









 -(void)selectionViewPushDetailController:(Model *)model
{
    GiftDetailViewController *detailVC=[[GiftDetailViewController alloc]init];
    detailVC.model=model;
    detailVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}
-(void)tapActionOnePushDetailController
{
    TapActionViewController *tapOneVC=[[TapActionViewController alloc]init];
    [self.navigationController pushViewController:tapOneVC animated:YES];
}
-(void)tapActionTwoPushDetailController
{
    TapActiononeViewController *tapTwoVC=[[TapActiononeViewController alloc]init];
    [self.navigationController pushViewController:tapTwoVC animated:YES];
}
-(void)cyclePushDetailController:(CycleModel *)model
{
    ImageViewController *imageVC=[[ImageViewController alloc]init];
    imageVC.model=model;
    [self.navigationController pushViewController:imageVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
